package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.domain.entity.MaterialType;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.service.IMaterialTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料类型Controller
 *
 * @author frcdi.qms
 * @date 2023-09-08
 */
@Api(tags = "物料类型")
@RestController
@RequestMapping("/masterData/materialType")
public class MaterialTypeController extends BaseController
{
    @Autowired
    private IMaterialTypeService materialTypeService;

    /**
     * 查询物料类型列表
     */
    @ApiOperation("查询物料类型列表")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:list')")
    @GetMapping("/list")
    public AjaxResult list(MaterialType materialType)
    {
        List<MaterialType> list = materialTypeService.selectMaterialTypeList(materialType);
        return success(list);
    }

    /**
     * 导出物料类型列表
     */
    @ApiOperation("导出物料类型列表")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialType materialType)
    {
        List<MaterialType> list = materialTypeService.selectMaterialTypeList(materialType);
        ExcelUtil<MaterialType> util = new ExcelUtil<MaterialType>(MaterialType.class);
        util.exportExcel(response, list, "物料类型数据");
    }

    /**
     * 获取物料类型详细信息
     */
    @ApiOperation("获取物料类型详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(materialTypeService.selectMaterialTypeById(id));
    }

    /**
     * 新增物料类型
     */
    @ApiOperation("新增物料类型")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:add')")
    @PostMapping
    public AjaxResult add(@RequestBody MaterialType materialType)
    {
        if (!materialTypeService.checkMaterialTypeNameUnique(materialType))
        {
            return error("新增物料类型'" + materialType.getMaterialTypeName() + "'失败，名称已存在");
        }
        materialType.setCreateBy(getUsername());
        return toAjax(materialTypeService.insertMaterialType(materialType));
    }

    /**
     * 修改物料类型
     */
    @ApiOperation("修改物料类型")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialType materialType)
    {
        Long typeId = materialType.getId();
        if (!materialTypeService.checkMaterialTypeNameUnique(materialType))
        {
            return error("修改物料类型'" + materialType.getMaterialTypeName() + "'失败，名称已存在");
        }
        else if (materialType.getParentId().equals(typeId))
        {
            return error("修改物料类型'" + materialType.getMaterialTypeName()+ "'失败，上级类型不能是自己");
        }
        materialType.setUpdateBy(getUsername());
        return toAjax(materialTypeService.updateMaterialType(materialType));
    }

    /**
     * 删除物料类型
     */
    @ApiOperation("删除物料类型")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:remove')")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        if (materialTypeService.hasChildById(id)) {
            return warn("存在下级,不允许删除");
        }
        // 检查是否存在物料
        if (materialTypeService.checkMaterialTypeExistMaterial(id)) {
            return warn("存在物料,不允许删除");
        }
        return toAjax(materialTypeService.deleteMaterialTypeById(id));
    }

    /**
     * 获取物料类型树列表
     */
    @ApiOperation("获取物料类型树列表")
    @PreAuthorize("@ss.hasPermi('masterData:materialType:list')")
    @GetMapping("/materialTypeTree")
    public AjaxResult deptTree(MaterialType materialType)
    {
        return success(materialTypeService.selectMaterialTypeTreeList(materialType));
    }
}
